How to Make a Simple Math Quiz Game in Python

您所在的位置:网站首页 simple input and maths How to Make a Simple Math Quiz Game in Python

How to Make a Simple Math Quiz Game in Python

2024-07-14 16:38:00| 来源: 网络整理| 查看: 265

  Maxim Maeder · 4 min read · Updated mar 2023 · General Python Tutorials

Confused by complex code? Let our AI-powered Code Explainer demystify it for you. Try it out!

In this tutorial, we will make a simple maths game on the console with the PyInputPlus module. The main features of this simple game are adding points (like a score), multiple equation types (such as addition, subtraction, multiplication, and division), and the ability to stop the game.

To get started, since PyInputPlus is not a built-in module, we have to install it:

$ pip install PyInputPlus

Importing PyInputPlus and random:

# Imports import pyinputplus as pyip from random import choice

Related: How to Make a Hangman Game in Python.

Setting Up Variables

We continue by setting up some variables to use later.

The questionTypes list holds the operators which can be used in the equations; keep in mind that they have to be valid Python operators. You could add the modulo (%) or any other valid Python operator to the list to enable these operators in the game. These will be randomly chosen with the random.choice() function.

Next, we define a list called numberRange which holds all numbers that can appear in the equations. We can do this in one line.

Last but not least, we define a points variable, which starts with 0.

# Variables questionTypes = ['+', '-', '*', '/', '**'] numbersRange = [num for num in range(1, 20)] points = 0 Hints

To ensure that the user knows what he has to do, we print some hints about the game.

Later we will round Solutions because equations like 7 / 4 are impossible to write out.

We will also enable the user to stop the game after every question. That's why we mention it here.

# Hints print('Round down to one Number after the Comma.') print('When asked to press enter to continue, type stop to stop.\n') Generate Equation

We now enter the game loop, where we start by deciding on a question type. This is done with the random.choice() method from the random module. This will return one of the items from the questionTypes.

Then we build the equation where we also use the random.choice() to choose random items from the numbersRange list and insert them in this string.

After that, we used Python's great eval() function. It takes a string and evaluates it, and returns the solution. We save this to the solution variable; we later test this against what the user has written.

# Game Loop while True: # Deciding and generating question currenType = choice(questionTypes) promptEquation = str(choice(numbersRange)) + ' ' + currenType + ' ' + str(choice(numbersRange)) solution = round(eval(promptEquation), 1) Receiving Input

Next, we use the inputNum() method from the PyInputPlus Module. This function will test if the input was a number, and if not, it will ask again. We fill out its prompt parameter with our prompt string; keep in mind to add ' = ' so it makes sense for the user. We could not have done this before the eval() function because it would work that way.

# Getting answer from User answer = pyip.inputNum(prompt=promptEquation + ' = ') Giving Feedback

After we receive the user input, we test it against the solution returned by the eval() function. If they match, we raise the points by one and print out a nice comment and the new point number.

If it's wrong, we lower the points by one and print out the right solution.

# Feedback and Points if answer == solution: points += 1 print('Correct!\nPoints: ',points) else: points -= 1 print('Wrong!\nSolution: '+str(solution)+'\nPoints: ',points) Stopping the Game

Last but not least, we halt the game after every question. If the user presses enter, it continues. For that to work, we have to set blank to True but if the user types stop, the game will stop.

# Stopping the Game if pyip.inputStr('Press "Enter" to continue', blank=True) == 'stop': break # Some Padding print('\n\n')

Let's run it:

$ python simple_math_game.py Round down to one Number after the Comma. When asked to press enter to continue, type stop to stop. 5 ** 4 = 625 Correct! Points: 1 Press "Enter" to continue 9 ** 18 = 190 Wrong! Solution: 150094635296999121 Points: 0 Press "Enter" to continue 7 - 17 = -10 Correct! Points: 1 Press "Enter" to continue stop

Awesome! Now you know how to make a simple console math game with PyInputPlus. You can get the complete code here.

Learn also: How to Generate Random Data in Python

Happy coding ♥

Just finished the article? Now, boost your next project with our Python Code Generator. Discover a faster, smarter way to code.

View Full Code Explain The Code for Me Sharing is caring!

Read Also How to Make a Hangman Game in Python

Learn how to make a hangman game to guess a word in Python using the standard library.

How to Make a Text Adventure Game in Python

Learn how to make a simple text adventure game with Python using the os, json, and pyinputplus modules.

How to Make a Chess Game with Pygame in Python

Learn how you can build a chess game from scratch with Python classes and pygame library in Python.

Comment panel

Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭